package com.sanuo.pico.utils;

import static com.menu.app.util.FunctionCode.START1;
import static com.menu.app.util.FunctionCode.START2;

import com.menu.app.MyApp;
import com.menu.app.db.WarnDao;
import com.menu.app.http.bean.ReportBean;
import com.menu.app.http.req.WarnReportReq;
import com.menu.app.it.ReqCallback;
import com.menu.app.util.Utils;
import com.menu.app.util.port.SerialManage;

import java.util.HashMap;

public class Command {

    public final static void send(String str) {
        String[] ss = str.split(" ");
        byte[] b = new byte[ss.length];
        for (int i = 0; i < ss.length; i++) {
            b[i] = (byte) Integer.parseInt(ss[i], 16);
        }
        send(b);
    }

    public final static void send(byte[] data) {
        byte[] touch_event = new byte[data.length + 3];
        touch_event[0] = START1;
        touch_event[1] = START2;
        touch_event[2] = (byte) (data.length);
        System.arraycopy(data, 0, touch_event, 3, data.length);
        SerialManage.getInstance().send(touch_event);
    }

    public final static void sendTouchEvent(Integer data_H, Integer data_L) {
        if (data_H == 0 && data_L == 0x65) {
            Const.isRent = true;
        } else {
            Const.isRent = false;
        }
        sendSeekEvent(1, data_H, data_L);
    }

    public final static void sendTouchEvent(Integer data) {
        sendSeekEvent(1, data);
    }

    public final static void sendSeekEvent(Integer addr, Integer data_H, Integer data_L) {
        byte[] tmp = {(byte) 0x83, (byte) 0x10, addr.byteValue(), 1, data_H.byteValue(), data_L.byteValue()};
        send(tmp);
    }

    public final static void sendSeekEvent(Integer addr, Integer data) {
        byte[] b = Utils.getByteFromInt(data);
        byte[] tmp = {(byte) 0x83, (byte) 0x10, addr.byteValue(), 1, b[0], b[1]};
        send(tmp);
    }

    public static void getHandleType() {
        byte[] tmp = {(byte) 0xb0};
        tmp = Crc16Utils.getData(tmp);
        byte[] touch_event = new byte[3 + tmp.length];
        touch_event[0] = 0x5a;
        touch_event[1] = (byte) 0xa5;
        touch_event[2] = (byte) tmp.length;
        System.arraycopy(tmp, 0, touch_event, 3, tmp.length);
        SerialManage.getInstance().send(touch_event);
    }

    public static void getRented() {
        byte[] tmp = {(byte) 0xb4, 0, 0, 0, 0, 0};
        tmp = Crc16Utils.getData(tmp);
        byte[] touch_event = new byte[3 + tmp.length];
        touch_event[0] = 0x5a;
        touch_event[1] = (byte) 0xa5;
        touch_event[2] = (byte) tmp.length;
        System.arraycopy(tmp, 0, touch_event, 3, tmp.length);
        SerialManage.getInstance().send(touch_event);
    }

    public static void setRented(long value) {
        byte[] b = Utils.getByteFromLong(value);
        byte[] tmp = {(byte) 0xb4, 1, b[0], b[1], b[2], b[3]};
        tmp = Crc16Utils.getData(tmp);
        byte[] touch_event = new byte[3 + tmp.length];
        touch_event[0] = 0x5a;
        touch_event[1] = (byte) 0xa5;
        touch_event[2] = (byte) tmp.length;
        System.arraycopy(tmp, 0, touch_event, 3, tmp.length);
        SerialManage.getInstance().send(touch_event);
    }

    public static void setWave(boolean is532) {
        byte[] tmp = {is532 ? (byte) 0xb3 : (byte) 0xb2};
        tmp = Crc16Utils.getData(tmp);
        byte[] touch_event = new byte[3 + tmp.length];
        touch_event[0] = 0x5a;
        touch_event[1] = (byte) 0xa5;
        touch_event[2] = (byte) tmp.length;
        System.arraycopy(tmp, 0, touch_event, 3, tmp.length);
        SerialManage.getInstance().send(touch_event);
    }

    public static void setParam(int energy, int hz, int red) {
        byte[] b = Utils.getByteFromInt(energy);
        byte[] tmp = {(byte) 0xb1, b[0], b[1], (byte) hz, (byte) red};
        tmp = Crc16Utils.getData(tmp);
        byte[] touch_event = new byte[3 + tmp.length];
        touch_event[0] = 0x5a;
        touch_event[1] = (byte) 0xa5;
        touch_event[2] = (byte) tmp.length;
        System.arraycopy(tmp, 0, touch_event, 3, tmp.length);
        SerialManage.getInstance().send(touch_event);
    }

    public static void reportWarn(int code) {
        HashMap<String, String> map = new HashMap<>();
        map.put("error", code + "");
        map.put("time", System.currentTimeMillis() + "");
        try {
            map.put("customId", MyApp.getApp().getDoc().serverId);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        new WarnReportReq(map, null).req(new ReqCallback<ReportBean>() {
            @Override
            public void callback(boolean success, ReportBean bean) {
                if (success && bean.isValid()) {
                    ;
                } else {
                    new WarnDao().insert(map, null);
                }
            }
        });
    }
}
